package com.lottery.im.module.main.adapter;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.lottery.im.R;
import com.lottery.im.module.main.ui.ImagePreviewActivity;
import com.lottery.im.widget.LoadingView;
import com.lottery.im.widget.photoview.PhotoView;
import com.lottery.im.widget.photoview.PhotoViewAttacher;
import java.io.File;
import java.util.List;

/**
 * 图片查看viewpager适配器
 */
public class PreviewImageAdapter extends PagerAdapter {

  private List<String> mPicPaths;
  //用来区别图片来源 是url还是本地文件
  private int picType;

  private PhotoViewAttacher.OnPhotoTapListener mPhotoTapListener;

  public PreviewImageAdapter(List<String> picPaths, int picType) {
    mPicPaths = picPaths;
    this.picType = picType;
  }

  public void setPhotoTapListener(PhotoViewAttacher.OnPhotoTapListener photoTapListener) {
    mPhotoTapListener = photoTapListener;
  }

  @Override public int getCount() {
    return mPicPaths.size();
  }

  @Override public View instantiateItem(ViewGroup container, int position) {

    View v = LayoutInflater.from(container.getContext()).inflate(R.layout.layout_loading_pic, null);
    PhotoView photoView = v.findViewById(R.id.pic);

    final LoadingView progressWheel = v.findViewById(R.id.loading);

    //photoView.setImageUri(sDrawables[position]);
    if (ImagePreviewActivity.PATH_TYPE == picType) {
      Glide.with(container.getContext()).load(new File(mPicPaths.get(position))).into(photoView);
    } else {

      progressWheel.setVisibility(View.VISIBLE);
      Glide.with(container.getContext())
          .load(mPicPaths.get(position))
          .apply(new RequestOptions().error(android.R.color.transparent))
          .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model,
                Target<Drawable> target,
                boolean isFirstResource) {
              progressWheel.setVisibility(View.GONE);
              return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target,
                DataSource dataSource, boolean isFirstResource) {
              progressWheel.setVisibility(View.GONE);
              return false;
            }
          })
          .into(photoView);
    }
    // Now just add PhotoView to ViewPager and return it
    container.addView(v);

    if (null != mPhotoTapListener) {
      photoView.setOnPhotoTapListener(mPhotoTapListener);
    }

    return v;
  }

  @Override public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
  }

  @Override public boolean isViewFromObject(View view, Object object) {
    return view == object;
  }
}